package com.zw.stock_sell_storage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zw.stock_sell_storage.dto.MyPageDto;
import com.zw.stock_sell_storage.dto.SaleDetailDto;
import com.zw.stock_sell_storage.dto.SaleInformationDto;
import com.zw.stock_sell_storage.dto.ex.QueryTimeScopeDtoEx;
import com.zw.stock_sell_storage.dto.ex.SaleInformationDtoEx;
import com.zw.stock_sell_storage.mapper.SaleInformationMapper;
import com.zw.stock_sell_storage.service.ISaleDetailService;
import com.zw.stock_sell_storage.service.ISaleInformationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SaleInformationServiceImpl extends ServiceImpl<SaleInformationMapper, SaleInformationDto> implements ISaleInformationService {
    @Autowired
    SaleInformationMapper saleInformationMapper;
    @Autowired
    ISaleDetailService iSaleDetailService;
    @Override
    public Page<SaleInformationDto> querySaleInformationList(MyPageDto<QueryTimeScopeDtoEx<SaleInformationDto>> myPageDto) {
        QueryWrapper queryWrapper = new QueryWrapper();
        if (myPageDto.getData().getData().getSaleCustomerName()!=null&&!"".equals(myPageDto.getData().getData().getSaleCustomerName())){
            queryWrapper.like("sale_customer_name",myPageDto.getData().getData().getSaleCustomerName());

        }
        if (myPageDto.getData().getStartTime()!=null&&!"".equals(myPageDto.getData().getStartTime())){
            queryWrapper.ge("sale_time",myPageDto.getData().getStartTime());

        }
        if (myPageDto.getData().getEndTime()!=null&&!"".equals(myPageDto.getData().getEndTime())){
            queryWrapper.le("sale_time",myPageDto.getData().getEndTime());

        }

        return saleInformationMapper.selectPage(new Page<>(myPageDto.getCurrentPage(),myPageDto.getPageSize()),queryWrapper);
    }

    @Override
    public SaleInformationDtoEx querySaleInformationById(int saleId) {
        SaleInformationDto saleInformationDto = saleInformationMapper.selectById(saleId);
        List<SaleDetailDto> saleDetailList = iSaleDetailService.querySaleDetailByPurchasingId(saleId);
        SaleInformationDtoEx saleInformationDtoEx = new SaleInformationDtoEx();
        saleInformationDtoEx.setSaleId(saleInformationDto.getSaleId());
        saleInformationDtoEx.setSaleCustomerName(saleInformationDto.getSaleCustomerName());
        saleInformationDtoEx.setSaleAgent(saleInformationDto.getSaleAgent());
        saleInformationDtoEx.setSaleTime(saleInformationDto.getSaleTime());
        saleInformationDtoEx.setSaleOrderMoney(saleInformationDto.getSaleOrderMoney());
        saleInformationDtoEx.setSaleDeliveryAddress(saleInformationDto.getSaleDeliveryAddress());
        saleInformationDtoEx.setSaleDetailList(saleDetailList);
        return saleInformationDtoEx;
    }

    @Override
    @Transactional
    public boolean addSaleInformation(SaleInformationDto saleInformationDto) {
        saleInformationMapper.insert(saleInformationDto);
        return true;
    }
}
